# This is a sample Python script.
import logging
import sys
import time
import traceback

from PyQt5.QtWidgets import QApplication, QMessageBox

from base.synthetical import SyntheticalGui
from utils.rsa_util import rsa_decrypt

# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.


logging.basicConfig(filename='crash.log', level=logging.ERROR,
                    format='%(asctime)s - %(levelname)s - %(message)s')


# 全局的异常处理函数
def exception_handler(exc_type, exc_value, exc_traceback):
    """
    全局异常处理函数，用于记录未捕获的异常。
    """
    tb_str = traceback.format_exception(exc_type, exc_value, exc_traceback)
    tb_str = "".join(tb_str)
    logging.error("Uncaught exception:\n" + tb_str)


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    # 将全局异常处理函数设置为sys.excepthook
    sys.excepthook = exception_handler
    app = QApplication([])
    gui = SyntheticalGui()
    timestamp = rsa_decrypt(gui.ui.login_token)
    if int(timestamp) > int(time.time()):
        gui.ui.show()
        status = app.exec_()
        print("程序执行完成，执行状态为：", status)
        if status == 0:
            app.closeAllWindows()
            app.quit()
            gui.ui.close()
    else:
        QMessageBox.about(gui.ui, "错误", str('服务异常崩溃...'))
